<template>
    <table>
        <td>
            <tr>
                <label for="">书名：</label>
                <td><input type="text" placeholder="请输入书名：" v-model="allData.bookName"></td>
            </tr>
            <tr>
                <label for="">作者：</label>
                <td><input type="text" placeholder="请输入作者:" v-model="allData.author"></td>
            </tr>
            <tr>
              <td>  <input type="button" value="保存" @click="btnSave"></td>
              <td>  <input type="button" value="取消" @click="btnQx"></td>
            </tr>
        </td>
    </table>
</template>
<script setup>
import { Addit, Editit, GitId } from '@/api/bookApi';
import { onMounted, reactive } from 'vue';
import { useRoute, useRouter } from 'vue-router';
let allData=reactive({
    bookName:'',
    author:''
})
let router=useRouter()
let route=useRoute()
async function btnSave(){
    let id =route.params.id || 0
    let res
    if(id>0){
        res=await Editit(id,allData)
    }else{
        res=await Addit(allData)
    }
    if(res.code = 1000){
        router.push('/book')
    }else{
        console.log(res.msg);
    }
}
function btnQx(){
    router.push('/book')
}
onMounted(async()=>{
    let id= route.params.id || 0
    if(id>0){
        let res=await GitId(id)
        console.log(res);
        
        allData.bookName=res.data.bookName
        allData.author=res.data.author
    }
})
</script>